package com.sttk.fastMark.camera;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.widget.FrameLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.sttk.fastMark.camera.activity.WebActivity;
import com.sttk.fastMark.camera.basic.utils.StatusBarUtils;
import com.sztle.admodule.IPolicyDialogCallback;
import com.sztle.admodule.apiprocessor.OutCommProcessor;
import com.sztle.admodule.apiprocessor.listener.IStartProgress;
import com.sztle.admodule.control.manager.ControlManager;
import com.sztle.admodule.myutils.PolicyDialogHelper;
import com.sztle.admodule.outstatus.AppStatusProcessor;


public class SplashActivity extends AppCompatActivity {

    private static final long SPLASH_DELAY = 1500; // 1.5 秒

    private FrameLayout adContainer;
    private SeekBar progressLine;

    private TextView tvDevInfo;

    protected void hideBottomUIMenu() {
        int flags;
        int curApiVersion = Build.VERSION.SDK_INT;
        // This work only for android 4.4+
        if (curApiVersion >= Build.VERSION_CODES.KITKAT) {

            flags = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;

        } else {
            // touch the screen, the navigation bar will show
            flags = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
                    View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
        }
        int originFlag = getWindow().getDecorView().getSystemUiVisibility();
        flags |= originFlag;
        getWindow().getDecorView().setSystemUiVisibility(flags);
    }


    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        hideBottomUIMenu();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_splash);
        StatusBarUtils.with(this).init();
        StatusBarUtils.setStatusBarTranslate(this);
        //设置状态栏字体颜色
        StatusBarUtils.setSystemStatusTextColor(false, this);

        adContainer = findViewById(R.id.ad_container);
        progressLine = findViewById(R.id.progress_line);
        tvDevInfo = findViewById(R.id.tv_dev_info);
        checkWaitGetConfig();
    }

    protected ValueAnimator animator;

    //todo list 启动页一进来开始等待获取配置
    private void checkWaitGetConfig(){
        animator = ValueAnimator.ofInt(0, 100);
        animator.setDuration(5000);
        animator.setInterpolator(new LinearInterpolator());
        animator.addListener(new AnimatorListenerAdapter() {

            @Override
            public void onAnimationEnd(Animator animation) {
                if (ControlManager.canStartInitSdk()) {
                    //获取配置，判断可以启动三方sdk初始化
                    startInitSdkAndLoading();
                    return;
                }
                //获取配置后，表明需要先弹隐私协议框
                showPrivacyDialog();
            }
        });

        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(@NonNull ValueAnimator animation) {
                //不断检测是否已经获取配置
                progressLine.setProgress((Integer) animation.getAnimatedValue());
                //不断检测是否获取到了服务配置
                if (AppStatusProcessor.isGetAppServerConfig()) {
                    //获取到配置后，则停止进度条，进行下一步判断是否弹隐私协议框
                    if (!animator.isPaused()) {
                        animator.pause();
                        animator.end();
                    }
                }
            }
        });
        animator.start();
    }

    //todo list 启动三方sdk初始化,启动进度条时间设置为10s
    private void startInitSdkAndLoading(){
        //todo list 启动三方sdk初始化
        AppStatusProcessor.statusInitSdk(this);
        animator = ValueAnimator.ofInt(10, 100);
        animator.setDuration(10000);
        animator.setInterpolator(new DecelerateInterpolator());
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                if (!TextUtils.isEmpty(OutCommProcessor.getSsid())) {
                    //获取到ssid后，显示ssid（哈希后的oaid）
                    tvDevInfo.setText(OutCommProcessor.getSsid());
                }
                int value = (int) animation.getAnimatedValue();
                progressLine.setProgress(value);
                //todo list 启动过程中需不断去做一些风控检测,IStartProgress为风控过程中控制启动页面结束或者暂停或者恢复继续 传递实现方法。
                //adContainer 为开屏广告使用的容器。
                AppStatusProcessor.statusLoadingProgressCheck(SplashActivity.this, adContainer, new IStartProgress() {
                    @Override
                    public void cancelLoadingProgress() {
                        //todo list 启动进度条取消，则结束启动页面
                        if (animator!= null) {
                            animator.cancel();
                        }
                    }

                    @Override
                    public void pauseLoadingProgress() {
                        //todo list 暂停启动页面
                        if (animator!= null) {
                            animator.pause();
                        }
                    }

                    @Override
                    public void resumeLoadingProgress() {
                        //todo list 恢复启动页面
                        if (animator!= null) {
                            animator.resume();
                        }
                    }
                });
            }
        });

        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                progressLine.setProgress(100);
                gotoNextPage();
            }

            @Override
            public void onAnimationCancel(Animator animation) {
                super.onAnimationCancel(animation);
                gotoNextPage();
            }
        });
        animator.start();
    }

    //todo list 弹隐私协议框
    private void showPrivacyDialog() {
        PolicyDialogHelper.showPolicyDialog(this, new IPolicyDialogCallback() {
            @Override
            public void onSure() {
                //同意后，开始加载三方sdk
                startInitSdkAndLoading();
            }

            @Override
            public void onCancel() {
                finish();
            }

            @Override
            public void onClickUserPolicy() {
                //todo list 跳转用户协议页面，开发人员可自己定义页面及跳转方式
                Intent bundle = new Intent(SplashActivity.this, WebActivity.class);
                bundle.putExtra("type", 0);
                startActivity(bundle);
            }

            @Override
            public void onClickPrivacyPolicy() {
                //todo list 跳转隐私协议页面，开发人员可自己定义页面及跳转方式
                Intent bundle = new Intent(SplashActivity.this, WebActivity.class);
                bundle.putExtra("type", 1);
                startActivity(bundle);
            }
        });
    }

    private void gotoNextPage() {
        if (animator != null) {
            //todo list 移除一切监听，防止多次跳转到主页面。
            animator.removeAllListeners();
        }
        Intent bundle = new Intent(SplashActivity.this, HomeActivity.class);
        startActivity(bundle);
        finish();
    }
}